﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Edge.NetApi;
using System.Configuration;
using Microsoft.Win32;

namespace LG_RECV_HEART
{
    class Program
    {
        //private static Socket socketSend;//发送
        private static MemDb db = new MemDb();
        private static string msg = string.Empty;
        private static string F_R1_L2_Total_Stiffness = ConfigurationManager.AppSettings["F_R1_L2_Total_Stiffness"];
        private static string F_R1_L2_DS_Stiffness = ConfigurationManager.AppSettings["F_R1_L2_DS_Stiffness"];
        private static string F_R1_L2_OS_Stiffness = ConfigurationManager.AppSettings["F_R1_L2_OS_Stiffness"];
        static void Main(string[] args)
        {
            try
            {
                Logger<string>.Debug("程序开始运行...");
                //ServiceLog.Start();
                Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse("10.9.62.100");
                //创建对象端口
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32("23333"));

                socketWatch.Bind(point);//绑定端口号
                Console.WriteLine("信息:监听成功!");
                socketWatch.Listen(100);//允许连接的客户端数量
                                        //创建监听线程
                Thread thread = new Thread(Listen);
                thread.IsBackground = true;
                thread.Start(socketWatch);

                Thread L1_Sign_Thread = new Thread(L1_Sign_Method);
                L1_Sign_Thread.IsBackground = true;
                L1_Sign_Thread.Start();


                Thread L2_Sign_Thread = new Thread(L2_Sign_Method);
                L2_Sign_Thread.IsBackground = true;
                L2_Sign_Thread.Start();

                SetStartWithStartUp();

                Console.ReadLine();
                //  IsStart = true;
            }
            catch (Exception ex)
            {
                Logger<string>.Error(ex.ToString());
            }

        }

        #region 注册自动重启
        //程序的名称
        private const string SoftWareName = "LG_RECV_HEART";

        private static void SetStartWithStartUp()
        {
            string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            // 针对当前用户添加注册表启动项
            RegistryKey rk = Registry.CurrentUser;
            try
            {
                //注册表位置   
                RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");

                // 检测是否之前有设置自启动了，如果设置了，就看值是否一样
                string old_path = (string)rk2.GetValue(SoftWareName);

                if (!path.Equals(old_path))
                {
                    //写入值
                    rk2.SetValue(SoftWareName, path);
                    Console.WriteLine("设置开机自启成功");
                }
                else
                {
                    Console.WriteLine("已设置过开机自启");
                }

                rk2.Close();
                rk.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine($"开机自启动设置失败:{e}");
            }
        }

        #endregion

        /// <summary>
        /// 是否连接到服务器,true表示连接成功,false表示连接失败
        /// </summary>
        /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
        /// <returns></returns>
        public static bool PingIpOrDomainName(string strIpOrDName)
        {
            try
            {
                Ping objPingSender = new Ping();

                PingOptions objPinOptions = new PingOptions();

                objPinOptions.DontFragment = true;

                string data = "";

                byte[] buffer = Encoding.UTF8.GetBytes(data);

                int intTimeout = 120;

                PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);

                string strInfo = objPinReply.Status.ToString();

                if (strInfo == "Success")
                {
                    return true;
                }

                else
                {
                    return false;
                }

            }

            catch (Exception)
            {

                return false;

            }

        }

        private static void L1_Sign_Method()
        {
            bool Flag = false;
            while (true)
            {
                Thread.Sleep(10000);
                Flag = PingIpOrDomainName("192.168.1.10");
                if (Flag)
                {
                    db.Write("FORWARD_RECV.L1_Sign", 0);//0正常
                }
                else
                {
                    db.Write("FORWARD_RECV.L1_Sign", 1);//无变化赋值1
                }
            }



        }

        private static void L2_Sign_Method()
        {
            int L2_counter = Convert.ToInt32(db.Read("RR_R1_L2_RECV_SINGLE.F_R1_L2_Telegram_Counter"));
            int L2_HeartCounter = 0;
            if (!string.IsNullOrEmpty(db.Read("L2_HEART.F_R1_L2_Telegram_Counter").ToString()))
            {
                L2_HeartCounter = Convert.ToInt32(db.Read("L2_HEART.F_R1_L2_Telegram_Counter"));
            }

            int Cur_L2_HeartCounter = 0;
            while (true)
            {
                Thread.Sleep(300000);

                int Cur_L2_counter = Convert.ToInt32(db.Read("RR_R1_L2_RECV_SINGLE.F_R1_L2_Telegram_Counter"));
                if (L2_counter == Cur_L2_counter)
                {
                    Thread.Sleep(6000);
                    Cur_L2_HeartCounter = Convert.ToInt32(db.Read("L2_HEART.F_R1_L2_Telegram_Counter"));
                    if (L2_HeartCounter == Cur_L2_HeartCounter)
                    {
                        //db.Write("FORWARD_RECV.L2_Sign", 1);//无变化赋值1

                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_ID", "");
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Steel_Grade", "");
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_Thickness", 0);//板坯厚度 /10
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_Width", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_Length", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Total_Passes", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_R1_Passes", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Step_Cushion_No", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Heating_Furnace_No", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_UWrokroll_Dia", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_DWrokroll_Dia", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_USupportroll_Dia", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Dsupportroll_Dia", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Uwrokroll_No", "");
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Dwrokroll_No", "");
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Usupportroll_No", "");
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Dsupportroll_No", "");
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Acceleration_Ratio", 1.5);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", 0);//摩擦系数 /100
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", 0);//E1减宽量 /100
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", 0);//穿带加速度设定值 /100
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", 0);//R1稳定轧制速度设定值 /100
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", 0);//R1板坯塑性变形系数 /10
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", 0);//前滑值 /10000
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", 0);//入口厚度 /10
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", 0);//出口厚度 /10
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", 0);
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", 0);//出口板坯厚度 /10
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", 0);//辊缝设定值 /10
                        db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", 0);//辊缝倾斜设定值 /10


                    }
                    else
                    {
                        int L2_Send_Sign = Convert.ToInt32(db.Read("FORWARD_RECV.L2_SEND_Sign"));
                        if (L2_Send_Sign == 1)
                        {
                            db.Write("FORWARD_RECV.L2_Sign", 1);
                        }
                        else
                        {
                            db.Write("FORWARD_RECV.L2_Sign", 0);//有变化赋值0
                        }

                    }
                }
                else
                {
                    int L2_Send_Sign = Convert.ToInt32(db.Read("FORWARD_RECV.L2_SEND_Sign"));
                    if (L2_Send_Sign == 1)
                    {
                        db.Write("FORWARD_RECV.L2_Sign", 1);
                    }
                    else
                    {
                        db.Write("FORWARD_RECV.L2_Sign", 0);//有变化赋值0
                    }
                }

                L2_counter = Cur_L2_counter;
                L2_HeartCounter = Cur_L2_HeartCounter;

            }
        }

        private static void Listen(object o)
        {
            try
            {
                Socket socketWatch = o as Socket;
                while (true)
                {
                    Socket socketSend = socketWatch.Accept();//等待接收客户端连接

                    //开启一个新线程，执行接收消息方法
                    Thread r_thread = new Thread(Received);
                    r_thread.IsBackground = true;
                    r_thread.Start(socketSend);

                    //Thread s_th = new Thread(SendMsg);
                    //s_th.IsBackground = true;
                    //s_th.Start(socketSend);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("等待客户端监听发生异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 服务器端不停的接收客户端发来的消息
        /// </summary>
        /// <param name="o"></param>
        static void Received(object o)
        {
            try
            {
                Socket socketSend = o as Socket;
                bool Islog = true;
                //if (!socketSend.Connected) return;
                //Console.WriteLine("开始接收。");
                int counter = 1;//判断L2接收设定值变化使用
                while (Islog)
                {
                    try
                    {
                        //客户端连接服务器成功后，服务器接收客户端发送的消息
                        byte[] buffer = new byte[1024 * 1024 * 3];//通讯字节长度
                                                                  //实际接收到的有效字节数
                        int len = socketSend.Receive(buffer);
                        if (len == 0)
                        {
                            Console.WriteLine("接收字节为0。");
                            //continue;
                            break;
                        }
                        string strData = Encoding.UTF8.GetString(buffer, 0, len);//取得对方传过来的数据
                                                                                 //string strData = "2022000220221123141037RMRXRXRM00081306642513353013060  LGQX-Z               02400124508200551001234001233001586001545GC01059 GC01066 GC11003 GC11001 4000018350200020001031039807425128112752450206010870000010020750000000000000280030001952061110717127512872060155700000000013015670000000054840350035002550080712697128712751557105500000000019510610000000000000300048104107106115752127212831055060600000000033706070000000016950380060005271069308453128312770606043700000000046904510000000005000000000000000000000000000000000437000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";


                        // Logger<string>.Debug("接收字符串长度："+len.ToString());
                        string c_MsgID = strData.Substring(0, 8);//根据c_MsgID来判断是否是接收心跳还是实际数据
                        if (c_MsgID == "20220001")
                        {
                            Console.WriteLine("打印接收心跳字节------:" + strData + "---字节长度：" + len + "获取时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            Logger<string>.Debug("接收心跳字节：" + strData);
                            string datetime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff");//计算时间差 开始时间
                                                                                               // Logger<string>.Info(datetime);
                            string c_SendDate = strData.Substring(8, 8);
                            string c_SendTimeStamp = strData.Substring(16, 6);
                            string c_Sender = strData.Substring(22, 4);
                            string c_Reciever = strData.Substring(26, 4);
                            string c_MsgLen = strData.Substring(30, 6);
                            string c_SequenceNo = strData.Substring(36, 4);
                            string c_ETX = strData.Substring(40, 1);
                            db.Write("L2_HEART.F_R1_L2_Telegram_ID", c_MsgID);
                            db.Write("L2_HEART.c_SendDate", c_SendDate);
                            db.Write("L2_HEART.c_SendTimeStamp", c_SendTimeStamp);
                            db.Write("L2_HEART.c_Sender", c_Sender);
                            db.Write("L2_HEART.c_Reciever", c_Reciever);
                            db.Write("L2_HEART.F_R1_L2_Telegram_Length", c_MsgLen);
                            db.Write("L2_HEART.F_R1_L2_Telegram_Counter", c_SequenceNo);
                            db.Write("L2_HEART.c_ETX", c_ETX);
                            string datetimeend = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff");//结束时间
                                                                                                  // Logger<string>.Info("接收时间：" + datetimeend);

                            string S_c_MsgID = "20230003";
                            string S_c_Sender = "RMRX";
                            string S_c_Reciever = "RXRM";
                            // string c_RecieverMsgID = "20230003";
                            string S_c_MsgLen = "000049";
                            string S_c_ETX = "\n";
                            msg = S_c_MsgID + c_SendDate + c_SendTimeStamp + S_c_Sender + S_c_Reciever + S_c_MsgLen + c_SequenceNo + c_MsgID + S_c_ETX;
                            if (!string.IsNullOrEmpty(msg))
                            {
                                bool flag = Send(msg, socketSend);
                                if (flag)
                                {
                                    Console.WriteLine("回复心跳发送完成：" + msg);
                                    Logger<string>.Info("回复心跳发送完成：" + msg);
                                }
                                else
                                {
                                    Console.WriteLine("回复心跳发送失败：" + msg);
                                    Logger<string>.Info("回复心跳发送失败：" + msg);
                                }

                            }
                        }
                        if (c_MsgID == "20220002")
                        {
                            Console.WriteLine("打印接收设定字节------:" + strData + "---字节长度：" + len + "获取时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            Logger<string>.Debug("接收设定字节：" + strData);

                            #region L2下发设定值
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Telegram_ID", Convert.ToInt32(strData.Substring(0, 8)));
                            // db.Write("RR_R1_L2_RECV_ALL.c_SendDate", strData.Substring(8, 8));
                            //db.Write("RR_R1_L2_RECV_ALL.c_SendTimeStamp", strData.Substring(16, 6));
                            // db.Write("RR_R1_L2_RECV_ALL.c_Sender", strData.Substring(22, 4));
                            //db.Write("RR_R1_L2_RECV_ALL.c_Reciever", strData.Substring(26, 4));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Telegram_Length", Convert.ToInt32(strData.Substring(30, 6)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Telegram_Counter", Convert.ToInt32(strData.Substring(36, 4)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_ID", strData.Substring(40, 15).Trim());
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Steel_Grade", strData.Substring(55, 21).Replace("\0", "").Trim());
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_Thickness", Convert.ToSingle(strData.Substring(76, 5)) / 10);//板坯厚度 /10
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_Width", Convert.ToSingle(strData.Substring(81, 4)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_Length", Convert.ToSingle(strData.Substring(85, 5)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Total_Passes", Convert.ToInt32(strData.Substring(90, 1)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_R1_Passes", Convert.ToInt32(strData.Substring(90, 1)));

                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Step_Cushion_No", Convert.ToInt32(strData.Substring(91, 1)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Heating_Furnace_No", Convert.ToInt32(strData.Substring(92, 1)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_UWrokroll_Dia", Convert.ToSingle(strData.Substring(93, 6)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_DWrokroll_Dia", Convert.ToSingle(strData.Substring(99, 6)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_USupportroll_Dia", Convert.ToSingle(strData.Substring(105, 6)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Dsupportroll_Dia", Convert.ToSingle(strData.Substring(111, 6)));
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Uwrokroll_No", strData.Substring(117, 8).Trim());
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Dwrokroll_No", strData.Substring(125, 8).Trim());
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Usupportroll_No", strData.Substring(133, 8).Trim());
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Dsupportroll_No", strData.Substring(141, 8).Trim());
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Reserve_Int16_1", Convert.ToSingle(strData.Substring(612, 1)));// 修磨坏信号 修磨坏信号为3及以上则为修磨
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Reserve_Int16_2", Convert.ToSingle(strData.Substring(613, 1)));//回退坏信号为1 则为回退板坯 为0正常
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Reserve_Float_2", Convert.ToSingle(strData.Substring(614, 3)) / 10);//目标厚度
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Reserve_Int16_3", Convert.ToInt32(strData.Substring(617, 2)));//钢种族号
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Classes", strData.Substring(619, 1));//班次
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Groups", strData.Substring(620, 1));//班组




                            //string data1= strData.Substring(614,3);
                            //string data2 = strData.Substring(617,2);

                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Acceleration_Ratio", 1.5);
                            db.Write("RR_R1_L1_RECV.F_R1_L1_Reserve_Bool_1", false);

                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Total_Stiffness", F_R1_L2_Total_Stiffness);
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_DS_Stiffness", F_R1_L2_DS_Stiffness);
                            db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_OS_Stiffness", F_R1_L2_OS_Stiffness);

                            //当前到次
                            string cur_pass = strData.Substring(149, 1);

                            switch (cur_pass)
                            {
                                case "0":
                                    //P1
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 1);
                                    db.Write("RR_R1_L1_RECV.F_R1_L1_Reserve_Bool_1", true);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(150, 4)) / 100);//摩擦系数 /100
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(154, 4)) / 100);//E1减宽量 /100
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(158, 4)) / 100);//穿带加速度设定值 /100
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(162, 4)) / 100);//R1稳定轧制速度设定值 /100
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(166, 5)) / 10);//R1板坯塑性变形系数 /10
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(171, 4)) / 10000);//前滑值 /10000
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(175, 5)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(180, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(184, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(188, 4)) / 10);//入口厚度 /10
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(192, 4)) / 10);//出口厚度 /10
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(196, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(200, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(204, 4)) / 10);//出口板坯厚度 /10
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(208, 4)) / 10);//辊缝设定值 /10
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(212, 4)) / 10);//辊缝倾斜设定值 /10
                                    break;
                                case "1":
                                    //P2
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 2);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(216, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(220, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(224, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(228, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(232, 5)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(237, 4)) / 10000);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(241, 5)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(246, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(250, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(254, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(258, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(262, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(266, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(270, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(274, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(278, 4)) / 10);
                                    break;
                                case "2":
                                    //P3
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 3);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(282, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(286, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(290, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(294, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(298, 5)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(303, 4)) / 10000);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(307, 5)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(312, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(316, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(320, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(324, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(328, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(332, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(336, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(340, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(344, 4)) / 10);
                                    break;
                                case "3":
                                    //P4
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 4);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(348, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(352, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(356, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(360, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(364, 5)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(369, 4)) / 10000);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(373, 5)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(378, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(382, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(386, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(390, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(394, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(398, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(402, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(406, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(410, 4)) / 10);
                                    break;
                                case "4":
                                    //P5
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 5);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(414, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(418, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(422, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(426, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(430, 5)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(435, 4)) / 10000);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(439, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(444, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(448, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(452, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(456, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(460, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(464, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(468, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(472, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(476, 4)) / 10);
                                    break;
                                case "5":
                                    //P6
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 6);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(480, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(484, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(488, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(492, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(496, 5)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(501, 4)) / 10000);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(505, 5)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(510, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(514, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(518, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(522, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(526, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(530, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(534, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(538, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(542, 4)) / 10);
                                    break;
                                case "6":
                                    //P7
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No", 7);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Friction", Convert.ToSingle(strData.Substring(546, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_E1_Width_Reduction", Convert.ToSingle(strData.Substring(550, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Threading_Speed", Convert.ToSingle(strData.Substring(554, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Running_Speed", Convert.ToSingle(strData.Substring(558, 4)) / 100);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Plastic_DefCoeff", Convert.ToSingle(strData.Substring(562, 5)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_ForSlip_Ratio", Convert.ToSingle(strData.Substring(567, 4)) / 10000);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Rolling_Force", Convert.ToSingle(strData.Substring(571, 5)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Width", Convert.ToSingle(strData.Substring(576, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Width", Convert.ToSingle(strData.Substring(580, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Thick", Convert.ToSingle(strData.Substring(584, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Thick", Convert.ToSingle(strData.Substring(588, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Entry_Temp", Convert.ToSingle(strData.Substring(592, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Exit_Temp", Convert.ToSingle(strData.Substring(596, 4)));
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Bar_Length", Convert.ToSingle(strData.Substring(600, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap", Convert.ToSingle(strData.Substring(604, 4)) / 10);
                                    db.Write("RR_R1_L2_RECV_SINGLE.F_R1_L2_Gap_Leveling", Convert.ToSingle(strData.Substring(608, 4)) / 10);
                                    break;
                            }

                            //预留
                            // db.Write("RR_R1_L2_RECV_SINGLE.c_Spare", strData.Substring(612, 200));
                            // db.Write("RR_R1_L2_RECV_ALL.c_ETX", strData.Substring(812, 1));

                            db.Write("FORWARD_RECV.TCP_Counter", counter);//告诉发送端已接收到设定值
                            counter++;
                            if (counter == 1000)
                            {
                                counter = 1;
                            }
                            #endregion


                        }
                    }
                    catch (Exception ex)
                    {
                        Islog = false;
                        Console.WriteLine("接收客户端内容发生异常循环内:" + ex.Message);
                        Logger<string>.Debug("接收客户端内容发生异常循环内:：" + ex.Message);

                    }



                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("接收客户端内容发生异常:" + ex.Message);
                Logger<string>.Debug("接收客户端内容发生异常:" + ex.Message);
            }
        }


        private static bool Send(string msg, object o)
        {
            bool flag = false;
            try
            {
                Socket socketSend = o as Socket;
                // byte[] buffer = new byte[1024 * 1024 * 3];
                byte[] buffer = Encoding.UTF8.GetBytes(msg);
                socketSend.Send(buffer);
                flag = true;
                //msg = string.Empty;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Send方法出现异常" + ex.Message);
                Logger<string>.Debug(ex.Message);
            }
            return flag;
        }


    }
}

